CREATE TABLE wdb_query_log
(
  id_query NUMBER(11) CONSTRAINT pk_wdb_query_log PRIMARY KEY,
  "session" VARCHAR2(32),
  request VARCHAR2(40),
  id_user NUMBER(11),
  ts TIMESTAMP DEFAULT sysdate,
  "query" CLOB NOT NULL,
  "type" VARCHAR2(20) CHECK ("type" in ('select', 'insert', 'update', 'delete', 'replace', 'other')),
  success NUMBER(1) DEFAULT 1 NOT NULL
);

CREATE SEQUENCE seq_wdb_query_log
 INCREMENT BY 1
 START WITH 1
 NOMAXVALUE
 MINVALUE 0;

CREATE TRIGGER tr_wdb_query_log
 BEFORE INSERT ON wdb_query_log
 FOR EACH ROW
 begin
   select seq_wdb_query_log.nextval into :new.id_query from dual;
 end;
/

CREATE TABLE wdb_query_errors
(
  id_query NUMBER(11) CONSTRAINT pk_wdb_query_errors PRIMARY KEY,
  "class" VARCHAR2(50),
  message CLOB,
  "schema" VARCHAR2(64),
  "table" VARCHAR2(64),
  "column" VARCHAR2(64),
  "constraint" VARCHAR2(64),
  "value" CLOB,
  FOREIGN KEY (id_query) REFERENCES wdb_query_log (id_query)  ON DELETE CASCADE
);